package com.scommunity.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scommunity.domain.Image;
import com.scommunity.mapper.ImageMapper;
import com.scommunity.service.IImageService;
import com.scommunity.utils.MinioUtils;
import com.scommunity.utils.RedisUtils;
import com.scommunity.utils.SnowflakeIdWorker;
import io.minio.GetObjectResponse;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Service
public class ImageServiceImpl extends ServiceImpl<ImageMapper,Image> implements IImageService{

    @Autowired
    private ImageMapper imageMapper;

    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;
    @Autowired
    private MinioUtils minioUtils;

//    @Cacheable(value = "imageList")
    @Override
    public List<Image> getList() {
//        List<Image> imageList = redisUtils.get("imageList", List.class);
//        if (imageList == null){
//            imageList = imageMapper.selectList(null);
//            redisUtils.set("imageList",imageList);
//        }
        List<Image> imageList = imageMapper.selectList(null);
        return imageList;
    }

    @Override
    public void add(Image image) {
        image.setImageId(snowflakeIdWorker.nextId());
        imageMapper.insert(image);
    }

    @Override
    public String upload(MultipartFile file) {
        String fileName = minioUtils.putObject(file);
        return fileName;
    }

//    @Override
//    public void updateByImageId(Image image) {
//        Image imageBean = new Image();
//        imageBean.setImageId(image.getImageId());
//        imageMapper.update(imageBean,new QueryWrapper<Image>());
//    }

//    @CacheEvict(value = "imageList", allEntries = true)
//    @Override
//    public void updateByImagePath(String image) {
//        //条件
//        Image imageBean = new Image();
//        imageBean.setImagePath(image);
//        //sql拼接
//        imageMapper.update(imageBean,new QueryWrapper<Image>().eq("image_path",""));
//    }

//    @Override
//    public GetObjectResponse download(String fileName, HttpServletResponse response) {
//        return null;
//    }
}
